home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1997 / MacHack 1997.toast / Hacks / Hacks ’97 / Ventriloquist / source code / TalkerApp / PP Basic Starter.cp < prev    next >
Encoding:
Text File  |  1997-06-27  |  6.8 KB  |  255 lines  |  [TEXT/CWIE]

  1. // ===========================================================================
  2. //    PP Basic Starter.cp         ©1994-1997 Metrowerks Inc. All rights reserved.
  3. // ===========================================================================
  4. //
  5. //    This file contains the starter code for a basic PowerPlant application
  6.  
  7. #include "PP Basic Starter.h"
  8.  
  9. #include <LGrowZone.h>
  10. #include <LWindow.h>
  11. #include <PP_Messages.h>
  12. #include <PP_Resources.h>
  13. #include <PPobClasses.h>
  14. #include <UDrawingState.h>
  15. #include <UMemoryMgr.h>
  16. #include <URegistrar.h>
  17. #include <LEditField.h>
  18. #include "beeper.h"
  19. #include    "speech.proto.h"
  20. #include "OTdsp.h"
  21.  
  22. Boolean            gCanSpeak;
  23. short            gSelectedVoiceIndex;
  24. short            gNumberVoices;
  25. short            gSpeakingVoiceIndex;
  26. Boolean            gSpeakSelectionEnabled;
  27. SpeechChannel    gSpeechChannel;
  28. OSErr            gSpeechErrors[];
  29. Handle            gSpeechText;
  30. //VoiceSpec **        gVoices;
  31. long            gConnID;
  32.  
  33. // put declarations for resource ids (ResIDTs) here
  34.  
  35. const ResIDT    window_Sample        = 1;    // EXAMPLE
  36.  
  37. BeeperGlobals    *gInitGlobals;
  38.  
  39. // ===========================================================================
  40. //        • Main Program
  41. // ===========================================================================
  42.  
  43. void main(void)
  44. {
  45.                                     // Set Debugging options
  46.     SetDebugThrow_(debugAction_Alert);
  47.     SetDebugSignal_(debugAction_Alert);
  48.  
  49.     InitializeHeap(3);                // Initialize Memory Manager
  50.                                     // Parameter is number of Master Pointer
  51.                                     //   blocks to allocate
  52.     
  53.                                     // Initialize standard Toolbox managers
  54.     UQDGlobals::InitializeToolbox(&qd);
  55.     
  56.     new LGrowZone(20000);            // Install a GrowZone function to catch
  57.                                     //    low memory situations.
  58.  
  59.     Str31    Server = "\pTalker";
  60.     Str31    Zone = "\p*";
  61.     gConnID = OTdspOpen(Server, Zone);
  62.     CPPStarterApp    theApp;            // replace this with your App type
  63.     theApp.Run();
  64.     OTClose(gConnID);
  65. }
  66.  
  67.  
  68. // ---------------------------------------------------------------------------
  69. //        • CPPStarterApp             // replace this with your App type
  70. // ---------------------------------------------------------------------------
  71. //    Constructor
  72.  
  73. CPPStarterApp::CPPStarterApp()
  74. {
  75.     // Register functions to create core PowerPlant classes
  76.     
  77.     RegisterAllPPClasses();
  78. }
  79.  
  80.  
  81. // ---------------------------------------------------------------------------
  82. //        • ~CPPStarterApp            // replace this with your App type
  83. // ---------------------------------------------------------------------------
  84. //    Destructor
  85. //
  86.  
  87. CPPStarterApp::~CPPStarterApp()
  88. {
  89. }
  90.  
  91. // ---------------------------------------------------------------------------
  92. //        • StartUp
  93. // ---------------------------------------------------------------------------
  94. //    This function lets you do something when the application starts up
  95. //    without a document. For example, you could issue your own new command.
  96.  
  97. void
  98. CPPStarterApp::StartUp()
  99. {
  100. //    ObeyCommand(cmd_New, nil);        // EXAMPLE, create a new window
  101.     long    err;
  102.     SelectorFunctionUPP    ourGestaltUPP;
  103.     long    versionCheck;
  104. //    VoiceDescription    defaultVoiceInfo;
  105.     err = Gestalt(kDTS_Signature, (long *)&ourGestaltUPP);
  106.     if (err == noErr) {
  107.         err = CallSelectorFunctionProc(ourGestaltUPP, gestaltVersion, (long *)&versionCheck);
  108.         if (versionCheck != 0x0100) err = -1;
  109.         if (err == noErr) {
  110.             err = CallSelectorFunctionProc(ourGestaltUPP, kGestaltGetInitGlobals, (long *)&gInitGlobals);
  111. //            gCanSpeak = !gCanSpeak;
  112.             //get default voice
  113. //            err = GetVoiceDescription(nil, &defaultVoiceInfo, sizeof(defaultVoiceInfo));
  114. //            err = NewSpeechChannel(&defaultVoiceInfo.voice, &gSpeechChannel);
  115.         }
  116.     }
  117. }
  118.  
  119. // ---------------------------------------------------------------------------
  120. //        • ObeyCommand
  121. // ---------------------------------------------------------------------------
  122. //    Respond to commands
  123.  
  124. Boolean
  125. CPPStarterApp::ObeyCommand(
  126.     CommandT    inCommand,
  127.     void        *ioParam)
  128. {
  129.     Boolean        cmdHandled = true;
  130.  
  131.     switch (inCommand) {
  132.     
  133.         // Deal with command messages (defined in PP_Messages.h).
  134.         // Any that you don't handle will be passed to LApplication
  135.              
  136.         case cmd_New:
  137.                                         // EXAMPLE, create a new window
  138.             LWindow        *theWindow;
  139.             theWindow = LWindow::CreateWindow(window_Sample, this);    
  140.             theWindow->Show();
  141.             break;
  142.  
  143.  
  144.  
  145.         default:
  146.             cmdHandled = LApplication::ObeyCommand(inCommand, ioParam);
  147.             break;
  148.     }
  149.     
  150.     return cmdHandled;
  151. }
  152.  
  153. // ---------------------------------------------------------------------------
  154. //        • FindCommandStatus
  155. // ---------------------------------------------------------------------------
  156. //    This function enables menu commands.
  157. //
  158.  
  159. void
  160. CPPStarterApp::FindCommandStatus(
  161.     CommandT    inCommand,
  162.     Boolean        &outEnabled,
  163.     Boolean        &outUsesMark,
  164.     Char16        &outMark,
  165.     Str255        outName)
  166. {
  167.  
  168.     switch (inCommand) {
  169.     
  170.         // Return menu item status according to command messages.
  171.         // Any that you don't handle will be passed to LApplication
  172.  
  173.         case cmd_New:                    // EXAMPLE
  174.             outEnabled = true;            // enable the New command
  175.             break;
  176.  
  177.         default:
  178.             LApplication::FindCommandStatus(inCommand, outEnabled,
  179.                                                 outUsesMark, outMark, outName);
  180.             break;
  181.     }
  182. }
  183.  
  184.  
  185. // ---------------------------------------------------------------------------
  186. //        • ProcessNextEvent
  187. // ____________________________________________________________________________
  188. //
  189. void
  190. CPPStarterApp::ProcessNextEvent()
  191. {
  192.     long    res;
  193.     if(( gInitGlobals->lWriteBuf > gInitGlobals->lReadBuf ) || 
  194.         (( gInitGlobals->lWriteBuf == 0 ) && ( gInitGlobals->lReadBuf > 1 )))
  195.     {
  196.         res = OTSend( gConnID, gInitGlobals->asBuf[gInitGlobals->lReadBuf]);
  197.         gInitGlobals->asBuf[gInitGlobals->lReadBuf][0] = 0;
  198.         gInitGlobals->lReadBuf++;
  199.         if( gInitGlobals->lReadBuf > 4) gInitGlobals->lReadBuf = 0;
  200.     }
  201.     LApplication::ProcessNextEvent();
  202. }
  203.  
  204. // ----------------------------------------------------------------------------
  205. //         • Speak
  206. // ----------------------------------------------------------------------------
  207.  
  208. void
  209. CPPStarterApp::Speak(Str31 word)
  210. {
  211.     OSErr    err;
  212. //    VoiceDescription    defaultVoiceInfo;
  213.  
  214.     
  215. //    StopSpeaking();    //kill current speech if any
  216.     
  217.     //get default voice
  218. //    err = GetVoiceDescription(nil, &defaultVoiceInfo, sizeof(defaultVoiceInfo));
  219. //    if (noErr != err) {
  220. //        err = MakeVoiceSpec('\0\0\0\0', '\0\0\0\0', &defaultVoiceInfo.voice);
  221. //    }
  222.     /* Get a speech channel */
  223.     
  224. //    gSpeakingVoiceIndex = gSelectedVoiceIndex;
  225. //    MoveHHi((Handle)gVoices);
  226. //    HLock((Handle)gVoices);
  227. //    err = NewSpeechChannel(&defaultVoiceInfo.voice, &gSpeechChannel);
  228. //    HUnlock((Handle)gVoices);
  229. //    if (noErr != err) {
  230.     
  231.         /* The voice that the user selected doesn't work; let's try the rest */
  232.         
  233. //        HLock((Handle)gVoices);
  234. //        for (gSpeakingVoiceIndex = 0; gSpeakingVoiceIndex < gNumberVoices; gSpeakingVoiceIndex++) {
  235. //            err = NewSpeechChannel(&(*gVoices)[gSpeakingVoiceIndex], &gSpeechChannel);
  236. //            if (noErr == err) {
  237. //                break;
  238. //            }
  239. //        }
  240. //        HUnlock((Handle)gVoices);
  241. //        if (gSpeakingVoiceIndex == gNumberVoices) {
  242. //            StopSpeaking();
  243. //            break;
  244. //        }
  245. //    }
  246.     
  247.     /* Start speaking the new text */
  248.     
  249.     err = SpeakText(gSpeechChannel, (char *)&word[1], word[0]);
  250.     if (noErr != err) {
  251.         StopSpeaking();
  252.     }
  253.     
  254. }
  255.